package jp.tuyano.helo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class Update implements EntryPoint {
    private Label label;
    private TextBox getId;
    private Label id;
    private Label name;
    private TextBox mail;
    private TextBox tel;
    private TextArea memo;
    
    private final UpdatePDServiceAsync updateService =
            GWT.create(UpdatePDService.class);

    private static final String SERVER_ERROR = "SERVER ERROR!!";

    
    public void onModuleLoad() {
        label = new Label("please type new PersonalData.");
        HorizontalPanel bar = new HorizontalPanel();
        getId = new TextBox();
        Button fetchbtn = new Button("Fetch");
        fetchbtn.addClickHandler(new ClickHandler(){
            
            public void onClick(ClickEvent event) {
                updateService.getDataById(Long.valueOf(getId.getText()),
                        new AsyncCallback<MyDataObject>(){
                    
                    public void onFailure(Throwable caught) {
                        label.setText(SERVER_ERROR);
                    }
                    
                    public void onSuccess(MyDataObject result) {
                        id.setText(String.valueOf(result.getId()));
                        name.setText(result.getName());
                        mail.setText(result.getMail());
                        tel.setText(result.getTel());
                        memo.setText(result.getMemo());
                        label.setText("Fetch data now!");
                    }
                });
            }
        });
        bar.add(new Label("fetch ID:"));
        bar.add(getId);
        bar.add(fetchbtn);
        FlexTable panel = new FlexTable();
        id = new Label();
        panel.setText(0, 0, "id");
        panel.setWidget(0, 1, id);
        name = new Label();
        panel.setText(1, 0, "Name:");
        panel.setWidget(1, 1, name);
        mail = new TextBox();
        panel.setText(2, 0, "Mail:");
        panel.setWidget(2, 1, mail);
        tel = new TextBox();
        panel.setText(3, 0, "Tel:");
        panel.setWidget(3, 1, tel);
        memo = new TextArea();
        memo.setSize("200px", "100px");
        panel.setText(4, 0, "Memo:");
        panel.setWidget(4, 1, memo);
        Button button = new Button("Click");
        panel.setWidget(5, 1, button);
        button.addClickHandler(new ClickHandler(){
            
            public void onClick(ClickEvent event) {

                updateService.updateEntity(Long.valueOf(id.getText()),
                        name.getText(), mail.getText(), tel.getText(),
                        memo.getText(), new AsyncCallback<String>(){
                    
                    public void onFailure(Throwable caught) {
                        label.setText(SERVER_ERROR);
                    }
                    
                    public void onSuccess(String result) {
                        label.setText(result);
                    }
                });
            }
        });
        RootPanel.get("update_bar").add(bar);
        RootPanel.get("update_label").add(label);
        RootPanel.get("update_panel").add(panel);
    }
}
